package com.apes.fa.service;

import com.apes.fa.model.FaEnableSituation;
import com.apes.fa.respository.FaEnableSituationRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("faEnableSituationService")
public class FaEnableSituationService extends DomainService {

    @Autowired
    private FaEnableSituationRepository faEnableSituationRepository;

    /**
     * 保存
     */
    public FaEnableSituation save(SimpleRequest request){

        FaEnableSituation faEnableSituation = request.getO(FaEnableSituation.class);
        faEnableSituation.setState("create");
        return faEnableSituationRepository.saveAndFlush(faEnableSituation);

    }

    /**
     * 删除
     */
    public String delete(SimpleRequest request){

        FaEnableSituation faEnableSituation = request.getO(FaEnableSituation.class);
        faEnableSituationRepository.delete(faEnableSituation);
        return "删除成功！";

    }

    /**
     * 审核
     */
    public FaEnableSituation approve(SimpleRequest request){

        FaEnableSituation faEnableSituation = request.getO(FaEnableSituation.class);
        faEnableSituation.setState("approve");
        faEnableSituation.setApproveDate(new Date());
        faEnableSituation.setApproveUid(this.getPerson(request));
        if("启用".equals(faEnableSituation.getEnableState())){
            faEnableSituation.setEnabler(this.getPerson(request));
//            User user = (User) getCurrentUser(request);
//            String name = user.getName();
            faEnableSituation.setEnableDate(new Date());

        }else{
            faEnableSituation.setDeactivatePerson(this.getPerson(request));
            faEnableSituation.setDeactivateDate(new Date());
        }
        return faEnableSituationRepository.saveAndFlush(faEnableSituation);

    }

    /**
     * 根据卡片Id查全部
     */
    public List<FaEnableSituation> find(SimpleRequest request){

        String id = request.get("id");
        List<FaEnableSituation> faEnableSituations = faEnableSituationRepository.findAllByFaCardId(id);
        return faEnableSituations;

    }

}
